/******************** (C) COPYRIGHT 2013 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2013/12
* AUTHOR:				SA1
* IC:						SN32F240/230/220
* DESCRIPTION:	Flash related functions.
*____________________________________________________________________________
* REVISION	Date				User		Description
* 1.0				2013/12/17	SA1			First release
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include "Flash.h"
#include <string.h>
#include "WDT.h"
#include "PWM.h"
#include "CT16.h"
#include "..\LED\LED_Driver.h"
#include  "..\Project\FlashRom.h"
/*_____ D E C L A R A T I O N S ____________________________________________*/

extern uint8_t rROW_NO;
/*_____ D E F I N I T I O N S ______________________________________________*/


/*_____ M A C R O S ________________________________________________________*/


/*_____ F U N C T I O N S __________________________________________________*/

/*_____ MACROS ________________________________________________________*/

/*_____ FUNCTIONS __________________________________________________*/

/*****************************************************************************
** Descriptions: SubFLASH_ProgramPage. This Function Storage in Rom, but we 
** excute it in Ram by Function pointer.
**
** parameters: starting rom address to program , program size, source ram pointer to program
** Returned value: 0: Pass, 1: Fail
** Descriptor:
***************************************************************************/
void SubFLASH_ProgramPage (void) 
{ 
	uint16_t *P_Temp;
	//** start Flash Program 
	SN_FLASH->CTRL |= FLASH_START; 
	//** Excuting LED SCAN function while checking ISP flag.
	while(SN_FLASH->STATUS & FLASH_BUSY)
	{
		//** Polling Timer IRQ flag
		if(SN_CT16B3->RIS_b.MR0IF) //Time To LED Scan
		{			
			SN_CT16B3->IC = mskCT16_MR0IC;	//Clear MR0 match interrupt status
			
			SN_CT16B1->TMRCTRL &= mskCT16_CEN_DIS;
			SN_CT16B0->TMRCTRL &= mskCT16_CEN_DIS;
			SN_CT16B2->TMRCTRL &= mskCT16_CEN_DIS;
			SN_CT16B5->TMRCTRL &= mskCT16_CEN_DIS;
			
//			All_LedRow_OutputLow();	
			SN_GPIO0->BCLR |= ROW_P0_MASK;	
			SN_GPIO1->BCLR |= ROW_P1_MASK;	
			SN_GPIO2->BCLR |= ROW_P2_MASK;	
			SN_GPIO3->BCLR |= ROW_P3_MASK;
			
			SN_CT16B1->MR0 = PWMKEY|0;
			SN_CT16B1->MR1 = PWMKEY|0;	
			SN_CT16B1->MR2 = PWMKEY|0;
			SN_CT16B1->MR3 = PWMKEY|0;
			SN_CT16B1->MR4 = PWMKEY|0;
			SN_CT16B1->MR5 = PWMKEY|0;
			SN_CT16B1->MR6 = PWMKEY|0;
			SN_CT16B1->MR7 = PWMKEY|0;
			SN_CT16B1->MR8 = PWMKEY|0;
			SN_CT16B1->MR9 = PWMKEY|0;
			SN_CT16B1->MR10 = PWMKEY|0;	
			
			SN_CT16B2->MR1 = PWMKEY|0;
			SN_CT16B2->MR2 = PWMKEY|0;
			SN_CT16B2->MR3 = PWMKEY|0;
			SN_CT16B2->MR0 = PWMKEY|0;

			SN_CT16B0->MR0 = PWMKEY|0;
			SN_CT16B0->MR3 = PWMKEY|0;
			SN_CT16B0->MR2 = PWMKEY|0;
			SN_CT16B0->MR1 = PWMKEY|0;

			SN_CT16B0->TC = 0;
			SN_CT16B1->TC = 0;
			SN_CT16B2->TC = 0;
			SN_CT16B5->TC = 0;  
		
			if(rROW_NO == 0)
			{
				P_Temp =&iRPWM_Buf[0];		R_ROW1_HIGH
			}
			else if(rROW_NO == 1)
			{
				P_Temp =&iRPWM_Buf[21];		R_ROW2_HIGH	
			}
			else if(rROW_NO == 2)
			{
				P_Temp =&iRPWM_Buf[42];		R_ROW3_HIGH
			}
			else if(rROW_NO == 3)
			{
				P_Temp =&iRPWM_Buf[63];		R_ROW4_HIGH
			}
			else if(rROW_NO == 4)
			{
				P_Temp =&iRPWM_Buf[84];		R_ROW5_HIGH
			}
			else if(rROW_NO == 5)
			{
				P_Temp =&iRPWM_Buf[105];	R_ROW6_HIGH
			}
//			else if(rROW_NO == 6)
//			{
//				P_Temp =&iRPWM_Buf[126];	R_ROW7_HIGH
//			}
			else if(rROW_NO == 7)
			{
				P_Temp =&iGPWM_Buf[0];		G_ROW1_HIGH	
			}
			else if(rROW_NO == 8)
			{
				P_Temp =&iGPWM_Buf[21];		G_ROW2_HIGH
			}
			else if(rROW_NO == 9)
			{
				P_Temp =&iGPWM_Buf[42];		G_ROW3_HIGH	
			}
			else if(rROW_NO == 10)
			{
				P_Temp =&iGPWM_Buf[63];		G_ROW4_HIGH	
			}
			else if(rROW_NO == 11)
			{
				P_Temp =&iGPWM_Buf[84];		G_ROW5_HIGH	
			}
			else if(rROW_NO == 12)
			{
				P_Temp =&iGPWM_Buf[105];	G_ROW6_HIGH
			}
//			else if(rROW_NO == 13)
//			{
//				P_Temp =&iGPWM_Buf[126];	G_ROW7_HIGH
//			}
			else if(rROW_NO == 14)
			{
				P_Temp =&iBPWM_Buf[0];		B_ROW1_HIGH	
			}
			else if(rROW_NO == 15)
			{
				P_Temp =&iBPWM_Buf[21];		B_ROW2_HIGH	
			}
			else if(rROW_NO == 16)
			{
				P_Temp =&iBPWM_Buf[42];		B_ROW3_HIGH	
			}
			else if(rROW_NO == 17)
			{
				P_Temp =&iBPWM_Buf[63];		B_ROW4_HIGH	
			}
			else if(rROW_NO == 18)
			{
				P_Temp =&iBPWM_Buf[84];		B_ROW5_HIGH	
			}
			else if(rROW_NO == 19)
			{
				P_Temp =&iBPWM_Buf[105];	B_ROW6_HIGH	
			}
//			else if(rROW_NO == 20)
//			{
//				P_Temp =&iBPWM_Buf[126];	B_ROW7_HIGH	
//			}
	
			SN_CT16B1->MR4 = PWMKEY|(*(P_Temp+0));
			SN_CT16B1->MR5 = PWMKEY|(*(P_Temp+1));
			SN_CT16B1->MR8 = PWMKEY|(*(P_Temp+2));
			SN_CT16B2->MR1 = PWMKEY|(*(P_Temp+3));
			SN_CT16B2->MR2 = PWMKEY|(*(P_Temp+4));
			SN_CT16B2->MR3 = PWMKEY|(*(P_Temp+5));
			SN_CT16B1->MR6 = PWMKEY|(*(P_Temp+6));
			SN_CT16B1->MR7 = PWMKEY|(*(P_Temp+7));
			SN_CT16B1->MR9 = PWMKEY|(*(P_Temp+8));
			SN_CT16B2->MR0 = PWMKEY|(*(P_Temp+9));
			SN_CT16B0->MR0 = PWMKEY|(*(P_Temp+10));
			SN_CT16B1->MR10 = PWMKEY|(*(P_Temp+11));
			SN_CT16B0->MR3 = PWMKEY|(*(P_Temp+12));
			SN_CT16B0->MR2 = PWMKEY|(*(P_Temp+13));
			SN_CT16B0->MR1 = PWMKEY|(*(P_Temp+14));
			SN_CT16B1->MR0 = PWMKEY|(*(P_Temp+15));
			SN_CT16B1->MR1 = PWMKEY|(*(P_Temp+16));	
			SN_CT16B1->MR2 = PWMKEY|(*(P_Temp+17));	
			SN_CT16B1->MR3 = PWMKEY|(*(P_Temp+18));	
		
			SN_CT16B0->TMRCTRL |= mskCT16_CEN_EN;  
			SN_CT16B1->TMRCTRL |= mskCT16_CEN_EN;
			SN_CT16B2->TMRCTRL |= mskCT16_CEN_EN;
			SN_CT16B5->TMRCTRL |= mskCT16_CEN_EN;
		
			rROW_NO++;
			if(rROW_NO >= 21)
				rROW_NO = 0;
			
			//-----------------------------------------------------------------------------------
			//** End of current Projrct LED SCAN Algrithm
			//-----------------------------------------------------------------------------------
		} 
	} 
}
